package com.example.test.advice.第七章_泛型和反射.建议94_不能初始化泛型参数和数组;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class Foo <T>{
    //编译错误
    //private T t = new T();
    //改正
    private T t;
    //编译错误
    //private T[] array = new T[5];
    //改正
    private T[] array;

    private List<T> list = new ArrayList<T>();

    /**
     * 构造方方法中初始化泛型参数和泛型数组
     */
    public Foo() {
        try {
            Class<?> tType = Class.forName("");
            t = (T)tType.newInstance();
            array = (T[]) Array.newInstance(tType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
